package com.qqtech.qquan.sys.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.sys.dao.SysMenuDao;
import com.qqtech.qquan.sys.model.SysMenu;
import com.qqtech.qquan.sys.model.SysMenuQuery;
import com.qqtech.qquan.sys.model.SysMenuVo;

@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu>
		implements SysMenuService {
	public static final Logger log = LoggerFactory.getLogger(SysMenuServiceImpl.class);
	
	@Resource
	private SysMenuDao sysMenuDao;

	@Override
	protected BaseDao<SysMenu> getBaseDao() {
		return sysMenuDao;
	}
	
	@Override
	public SysMenuVo getByMenuId(java.lang.String menuId) {
		return sysMenuDao.getByMenuId(menuId);
	}

	@Override
	public List<SysMenuVo> queryListByParentId(SysMenuQuery sysMenu) {
		return sysMenuDao.queryListByParentId(sysMenu);
	}

	@Override
	public int move(String targetId, String sourceId, String point) {
		SysMenuVo target = this.getByMenuId(targetId);
		SysMenuVo source = this.getByMenuId(sourceId);
		if (target.getType().intValue() == 2 && point.equals("append")) { //叶子节点不允许追加子节点
			return 0;
		}
		Integer maxOrderIdx = sysMenuDao.getMaxOrderIdx();
		Integer nextOrderIdx = 1;
		if (null != maxOrderIdx) {
			nextOrderIdx = maxOrderIdx + 1;
		}
		if(point.equals("append")) {				//如果是追加操作,那么直接追加到父节点下
			source.setParentId(target.getMenuId());
			source.setOrderIdx(Long.valueOf(nextOrderIdx));
			sysMenuDao.updateById(source);
		} else {									//否则是排序操作,那么移动为与目标节点同级,再执行同级排序
			source.setParentId(target.getParentId());
			source.setOrderIdx(Long.valueOf(nextOrderIdx));
			sysMenuDao.updateById(source);
			this.sortForMove(target, source, point);
		}
		return 1;
	}
	
	/**
	 * 为移动操作排序
	 * @param target
	 * @param source
	 * @param point
	 */
	private void sortForMove(SysMenuVo target, SysMenuVo source,String point) {
		SysMenuQuery sysMenuQuery = new SysMenuQuery();
		sysMenuQuery.setParentId(target.getParentId());
		List<SysMenuVo> children = this.queryListByParentId(sysMenuQuery);
		int index = 0;
		for(;index<children.size(); index++) {
			SysMenuVo m = children.get(index);
			if (target.getMenuId().equals(m.getMenuId())) {
				if(point.equals("top")) {			//插入源节点到适当的位置
					children.add(index,source);
				} else if(point.equals("bottom")) {
					children.add(index+1,source);
					index=index+1;					//保存源节点的索引号
				}
				break;
			}
		}
		children.remove(children.size()-1);			//删除重复的源节点(固定为最后一个节点),因为前面循环中,插入了一个源节点
		
		Long orderIdx = source.getOrderIdx();		//保存源节点的排序号
		for(;index<children.size();index++) {		//重新设置源节点和之后的节点排序号
			SysMenuVo m = children.get(index);
			if(index<children.size()-1) {
				m.setOrderIdx(children.get(index+1).getOrderIdx());
			} else {
				m.setOrderIdx(orderIdx);
			}
			sysMenuDao.updateById(m);
		}
	}

	@Override
	public Integer getMaxOrderIdx() {
		Integer maxOrderIdx = sysMenuDao.getMaxOrderIdx();
		if (null == maxOrderIdx) {
			maxOrderIdx = 0;
		}
		return maxOrderIdx;
	}

	@Override
	public List<SysMenuVo> topMenuForRole(SysMenuQuery sysMenuQuery) {
		return sysMenuDao.topMenuForRole(sysMenuQuery);
	}

	@Override
	public List<SysMenuVo> menuForRole(SysMenuQuery sysMenuQuery) {
		return sysMenuDao.menuForRole(sysMenuQuery);
	}
}